package com.qiangesoft.pay.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.qiangesoft.pay.app.config.WxPayProperties;
import com.qiangesoft.pay.app.service.IPayService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Service
public class PayServiceImpl implements IPayService {

    @Autowired
    private WxPayProperties wxPayProperties;

    @Autowired
    private JsapiServiceExtension jsapiServiceExtension;

    @Override
    public PrepayWithRequestPaymentResponse pay(String openId, String outTradeNo, Integer totalFee, String description) {
        if (StrUtil.isBlank(openId)) {
            throw new RuntimeException("openId不能为空");
        }
        if (StrUtil.isBlank(outTradeNo)) {
            throw new RuntimeException("商户交易号不能为空");
        }
        if (totalFee == null || totalFee <= 0) {
            throw new RuntimeException("交易金额必须大于0");
        }
        if (StrUtil.isBlank(description)) {
            throw new RuntimeException("商品描述不能为空");
        }

        // todo 生成订单

        // 微信预支付逻辑
        PrepayRequest prepayRequest = new PrepayRequest();
        prepayRequest.setAppid(wxPayProperties.getAppId());
        prepayRequest.setMchid(wxPayProperties.getMchid());
        prepayRequest.setNotifyUrl(wxPayProperties.getPayNotifyUrl());
        Amount amount = new Amount();
        amount.setTotal(totalFee);
        prepayRequest.setAmount(amount);
        prepayRequest.setOutTradeNo(outTradeNo);
        prepayRequest.setDescription(description);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        prepayRequest.setPayer(payer);
        return jsapiServiceExtension.prepayWithRequestPayment(prepayRequest);
    }

    @Override
    public Transaction query(String outTradeNo) {
        QueryOrderByOutTradeNoRequest orderByOutTradeNoRequest = new QueryOrderByOutTradeNoRequest();
        orderByOutTradeNoRequest.setOutTradeNo(outTradeNo);
        orderByOutTradeNoRequest.setMchid(wxPayProperties.getMchid());
        return jsapiServiceExtension.queryOrderByOutTradeNo(orderByOutTradeNoRequest);
    }

    @Override
    public void close(String outTradeNo) {
        CloseOrderRequest closeOrderRequest = new CloseOrderRequest();
        closeOrderRequest.setOutTradeNo(outTradeNo);
        closeOrderRequest.setMchid(wxPayProperties.getMchid());
        jsapiServiceExtension.closeOrder(closeOrderRequest);
    }

    @Override
    public void notify(Transaction transaction) {
        Transaction.TradeStateEnum tradeState = transaction.getTradeState();
        if (Transaction.TradeStateEnum.SUCCESS.equals(tradeState)) {
            String successTime = transaction.getSuccessTime();
            if (StrUtil.isNotBlank(successTime)) {
                DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
                OffsetDateTime offsetDateTime = OffsetDateTime.parse(successTime, formatter);
                LocalDateTime payTime = offsetDateTime.toLocalDateTime();
            }
            // todo 支付成功，更新订单状态

        }
    }

}
